<?php
namespace app\beautiful\controller\miniapp\member\goods;
use app\BaseController;
use app\beautiful\logic\order\OrderLogic;
use app\beautiful\model\mall\GoodsModel;
use app\beautiful\model\mall\GoodsSkuModel;
use app\beautiful\model\logs\GoodsViewsModel;
use think\facade\Db;

class PreSale extends BaseController{
    public function getPreSaleGoodsList()
    {

        $where=[
            ['shop_id','=',$this->request->shopId],
            ['status','=',1],
            ['is_delete','=',0],
            ['is_open_yu','=',1],
        ];

        $order=['sort'=>'desc','fictitious_take_count'=>'desc','vip_price'=>'desc'];
        $datas=GoodsModel::where($where)
            ->order($order)
            ->field('goods_id,cover_img,name,original_price,quota,fictitious_take_count,is_open_yu,yu_days,yu_price')
            ->limit($this->request->limit)
            ->page($this->request->page)
            ->select()
            ->append(['pre_sale_time'])
            ->toArray();
        $return['datas']=$datas;

        return $this->success($return);
    }

    public function getPreSaleGoodsDetail()
    {
        $goods_id = $this->request->param('goods_id/d',0);

        if(null === $goods = GoodsModel::find($goods_id)) return $this->error('预售商品不存在');
        if($goods->shop_id != $this->request->shopId) return $this->error('预售商品不存在');
        if($goods->is_delete == 1) return $this->error('预售商品不存在');
        if($goods->is_open_yu != 1) return $this->error('预售商品不存在');
        if($goods->status != 1) return $this->error('预售商品已下架');

        $sku=GoodsSkuModel::where([['goods_id','=',$goods_id],['is_delete','=',0]])
                ->field('sku_id,name,original_price,yu_price,quota')
                ->select()->toArray() ?? [];

        $detail=[
            'goods_id'=>$goods_id,
            'name'=>$goods->name,
            'cover_img' => $goods->cover_img,
            'original_price'=>$goods->original_price,
            'quota'=>$goods->quota,
            'fictitious_take_count'=>$goods->fictitious_take_count,
            'yu_price'=>$goods->yu_price,
            'yu_days'=>$goods->yu_days,
            'is_have_sku'=>$goods->is_have_sku,
            'pre_sale_time' => $goods->pre_sale_time,
            'rotation'=>$goods->rotation,
            'goods_details'=>$goods->goods_details,
            'purchase_information' => $goods->purchase_information,
            'sku'=>$sku,
        ];
        //记录预售对应的产品
        $GoodsViewsModel = new GoodsViewsModel();
        $GoodsViewsModel->save([
            'shop_id'   => $this->request->shopId,
            'goods_id'  => $goods_id
        ]);
        return $this->success(['detail'=>$detail]);

    }

    public function createOrder()
    {
        $logistics_type=$this->request->param('logistics_type/d');
        $receiving_name=$this->request->param('receiving_name');
        $receiving_mobile=$this->request->param('receiving_mobile');
        $receiving_address_province=$this->request->param('receiving_address_province');
        $receiving_address_city=$this->request->param('receiving_address_city');
        $receiving_address_county=$this->request->param('receiving_address_county');
        $receiving_address_info=$this->request->param('receiving_address_info');
        $total_price = $this->request->param('total_price/f',0);
        $use_integral = $this->request->param('use_integral/d',0);
        $coupon_id = $this->request->param('coupon_id/d',0);
        $goods=$this->request->param('goods/a',[]);
        $params=[
            'member_id'=>$this->request->memberId,
            'logistics_type'=>$logistics_type,
            'receiving_name'=>$receiving_name,
            'receiving_mobile'=>$receiving_mobile,
            'receiving_address_province'=>$receiving_address_province,
            'receiving_address_city'=>$receiving_address_city,
            'receiving_address_county'=>$receiving_address_county,
            'receiving_address_info'=>$receiving_address_info,
            'total_price'=>$total_price,
            'use_integral'=>$use_integral,
            'coupon_id'=>$coupon_id,
            'goods'=>$goods,
        ];
        Db::startTrans();
        try{
            $order_logic = OrderLogic::init($this->request->shopId);
            $order_id=$order_logic['pre_sale']->createOrder($params);
            Db::commit();
            return $this->success(['order_id'=>$order_id]);
        } catch (\Exception $e){
            Db::rollback();
            return $this->error($e->getMessage());
        }


    }
}
